/*
 * AboutDialogs.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic;

import de.studiorutton.changelog.Changelog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;

public class AboutDialogs {
	private Activity activity;

	public final static int ABOUT_DIALOG_MAIN = 11;
	public final static int ABOUT_DIALOG_CREDITS = 12;
	public final static int ABOUT_DIALOG_CHANGELOG = 13;

	public AboutDialogs(Activity activity) {
		this.activity = activity;
	}

	public AlertDialog onCreateDialog(int id) {
		switch (id) {
		case ABOUT_DIALOG_MAIN:
			return mainAboutDialog();
		case ABOUT_DIALOG_CREDITS:
			return creditsDialog();
		case ABOUT_DIALOG_CHANGELOG:
			return changelogDialog(null);
		default:
			return null;
		}
	}

	public AlertDialog mainAboutDialog() {

		AlertDialog ad = new AlertDialog.Builder(activity)
				.setTitle(R.string.about_title)
				.setIcon(R.drawable.gerengdict)
				.setItems(R.array.about_main_items, mOnClickListener)
				.setNeutralButton(R.string.about_dismiss,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {

								/* User clicked Something so do some stuff */
							}
						}).create();
		return ad;
	}

	private DialogInterface.OnClickListener mOnClickListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {

			switch (which) {

			// Credits
			case 0:
				activity.showDialog(ABOUT_DIALOG_CREDITS);
				break;
			// Changelog
			case 1:
				activity.showDialog(ABOUT_DIALOG_CHANGELOG);
				break;
			// Feedback
			case 2:
				Intent sendIntent = new Intent(Intent.ACTION_SEND);
				sendIntent.putExtra(Intent.EXTRA_SUBJECT,
						"Emilio's Dictionary Feedback");
				sendIntent.putExtra(Intent.EXTRA_EMAIL,
						new String[] { "contact@studiorutton.de" });
				sendIntent.setType("message/rfc822");
				activity.startActivity(Intent.createChooser(sendIntent,
						"Feedback via:"));
				break;
			// Rating/Market
			case 3:
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri
						.parse("market://search?q=pname:de.studiorutton.gerengdic"));
				activity.startActivity(intent);
				break;
			case 4:
				String videoId = "ldEh0cyGVm4";
				Intent videoIntent = new Intent(Intent.ACTION_VIEW,
						Uri.parse("vnd.youtube:" + videoId));
				activity.startActivity(Intent.createChooser(videoIntent,
						"Story:"));
			default:

			}
		}
	};

	public AlertDialog creditsDialog() {
		AlertDialog.Builder adb = new AlertDialog.Builder(activity)
				.setTitle(R.string.about_title)
				.setIcon(R.drawable.gerengdict)
				.setNeutralButton(R.string.about_dismiss,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {

								/* User clicked Something so do some stuff */
							}
						});
		// FrameLayout fl = (FrameLayout) ad.findViewById(android.R.id.custom);
		// fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
		ScrollView sv = (ScrollView) LayoutInflater.from(activity).inflate(
				R.layout.about_credits, null);

		adb.setView(sv);
		AlertDialog ad = adb.create();
		return ad;
	}

	public AlertDialog changelogDialog(DialogInterface.OnClickListener listener) {
		if (listener == null) {
			listener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {

					/* User clicked Something so do some stuff */
				}
			};
		}

		AlertDialog.Builder adb = new AlertDialog.Builder(activity)
				.setTitle(R.string.about_title_changelog)
				.setIcon(R.drawable.gerengdict)
				.setNeutralButton(R.string.about_dismiss, listener);

		// FrameLayout fl = (FrameLayout) ad.findViewById(android.R.id.custom);
		// fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));

		Changelog cl = new Changelog(activity);
		try {
			cl.populateItems(activity);
		} catch (Exception e) {
			e.printStackTrace();
		}

		LinearLayout ll = (LinearLayout) LayoutInflater.from(activity).inflate(
				R.layout.changelog_list, null);
		adb.setView(ll);

		ListView lv = (ListView) ll.findViewById(R.id.changelog_list);
		lv.setAdapter(cl.getChangelogListAdapter());
		lv.setClickable(false);

		AlertDialog ad = adb.create();
		return ad;
	}
}
